console.log('🧪 测试银行操作完成后自动触发1450请求推送...');

// 模拟银行操作处理器
const mockBankHandlers = {
    // 1457：银币从银行取出
    1457: async (payload, helpers) => {
        console.log('🔍 [1457] 处理银币取出请求:', payload);

        // 模拟银行操作成功
        const result = { data: { success: true } };

        // 操作成功后，主动触发1450请求查询最新余额并推送给用户
        try {
            const balanceData = await helpers.characterService.getCharacterResources(
                helpers.selectedCharacterId
            );

            // 构造1450响应数据
            const balanceResponse = {
                act_id: 1450,
                code: 0,
                data: {
                    balanceGold: balanceData?.balanceGold || 0,
                    balanceMoney: balanceData?.balanceMoney || 0,
                    gold: balanceData?.gold || 0,
                    money: balanceData?.silver || 0,
                },
            };

            // 通过WebSocket网关推送给用户
            if (helpers.wsGateway) {
                helpers.wsGateway.broadcastToUser(helpers.userId, balanceResponse);
                console.log('✅ [1457] 成功推送1450余额更新给用户:', helpers.userId);
            } else {
                console.log('⚠️ [1457] wsGateway未提供，无法推送消息');
            }
        } catch (balanceError) {
            console.error('❌ [1457] 推送余额更新失败:', balanceError);
        }

        return {
            act_id: 1457,
            code: 0,
            msg: "银币取出成功",
            data: result.data,
        };
    },

    // 1459：银币存入银行
    1459: async (payload, helpers) => {
        console.log('🔍 [1459] 处理银币存入请求:', payload);

        // 模拟银行操作成功
        const result = { data: { success: true } };

        // 操作成功后，主动触发1450请求查询最新余额并推送给用户
        try {
            const balanceData = await helpers.characterService.getCharacterResources(
                helpers.selectedCharacterId
            );

            // 构造1450响应数据
            const balanceResponse = {
                act_id: 1450,
                code: 0,
                data: {
                    balanceGold: balanceData?.balanceGold || 0,
                    balanceMoney: balanceData?.balanceMoney || 0,
                    gold: balanceData?.gold || 0,
                    money: balanceData?.silver || 0,
                },
            };

            // 通过WebSocket网关推送给用户
            if (helpers.wsGateway) {
                helpers.wsGateway.broadcastToUser(helpers.userId, balanceResponse);
                console.log('✅ [1459] 成功推送1450余额更新给用户:', helpers.userId);
            } else {
                console.log('⚠️ [1459] wsGateway未提供，无法推送消息');
            }
        } catch (balanceError) {
            console.error('❌ [1459] 推送余额更新失败:', balanceError);
        }

        return {
            act_id: 1459,
            code: 0,
            msg: "银币存入成功",
            data: result.data,
        };
    },

    // 1460：金币从银行取出
    1460: async (payload, helpers) => {
        console.log('🔍 [1460] 处理金币取出请求:', payload);

        // 模拟银行操作成功
        const result = { data: { success: true } };

        // 操作成功后，主动触发1450请求查询最新余额并推送给用户
        try {
            const balanceData = await helpers.characterService.getCharacterResources(
                helpers.selectedCharacterId
            );

            // 构造1450响应数据
            const balanceResponse = {
                act_id: 1450,
                code: 0,
                data: {
                    balanceGold: balanceData?.balanceGold || 0,
                    balanceMoney: balanceData?.balanceMoney || 0,
                    gold: balanceData?.gold || 0,
                    money: balanceData?.silver || 0,
                },
            };

            // 通过WebSocket网关推送给用户
            if (helpers.wsGateway) {
                helpers.wsGateway.broadcastToUser(helpers.userId, balanceResponse);
                console.log('✅ [1460] 成功推送1450余额更新给用户:', helpers.userId);
            } else {
                console.log('⚠️ [1460] wsGateway未提供，无法推送消息');
            }
        } catch (balanceError) {
            console.error('❌ [1460] 推送余额更新失败:', balanceError);
        }

        return {
            act_id: 1460,
            code: 0,
            msg: "金币取出成功",
            data: result.data,
        };
    },

    // 1462：金币存入银行
    1462: async (payload, helpers) => {
        console.log('🔍 [1462] 处理金币存入请求:', payload);

        // 模拟银行操作成功
        const result = { data: { success: true } };

        // 操作成功后，主动触发1450请求查询最新余额并推送给用户
        try {
            const balanceData = await helpers.characterService.getCharacterResources(
                helpers.selectedCharacterId
            );

            // 构造1450响应数据
            const balanceResponse = {
                act_id: 1450,
                code: 0,
                data: {
                    balanceGold: balanceData?.balanceGold || 0,
                    balanceMoney: balanceData?.balanceMoney || 0,
                    gold: balanceData?.gold || 0,
                    money: balanceData?.silver || 0,
                },
            };

            // 通过WebSocket网关推送给用户
            if (helpers.wsGateway) {
                helpers.wsGateway.broadcastToUser(helpers.userId, balanceResponse);
                console.log('✅ [1462] 成功推送1450余额更新给用户:', helpers.userId);
            } else {
                console.log('⚠️ [1462] wsGateway未提供，无法推送消息');
            }
        } catch (balanceError) {
            console.error('❌ [1462] 推送余额更新失败:', balanceError);
        }

        return {
            act_id: 1462,
            code: 0,
            msg: "金币存入成功",
            data: result.data,
        };
    },
};

// 模拟WebSocket网关
const mockWsGateway = {
    broadcastToUser: (userId, payload) => {
        console.log(`📡 [WebSocket] 推送给用户 ${userId}:`, payload);
        return true;
    }
};

// 模拟角色服务
const mockCharacterService = {
    getCharacterResources: async (characterId) => {
        console.log(`🔍 [CharacterService] 查询角色 ${characterId} 的资源信息`);
        // 返回模拟的余额数据
        return {
            balanceGold: 1000,
            balanceMoney: 5000,
            gold: 200,
            silver: 1000
        };
    }
};

// 测试函数
async function testBankOperations() {
    console.log('\n=== 测试银行操作自动触发1450推送 ===\n');

    const testCases = [
        { code: 1457, name: '银币取出', payload: { num: 100, pwd: '123456' } },
        { code: 1459, name: '银币存入', payload: { num: 200 } },
        { code: 1460, name: '金币取出', payload: { num: 50, pwd: '123456' } },
        { code: 1462, name: '金币存入', payload: { num: 100 } },
    ];

    for (const testCase of testCases) {
        console.log(`\n--- 测试 ${testCase.name} (${testCase.code}) ---`);

        try {
            const result = await mockBankHandlers[testCase.code](testCase.payload, {
                userId: 12345,
                selectedCharacterId: 67890,
                characterService: mockCharacterService,
                wsGateway: mockWsGateway,
            });

            console.log(`✅ ${testCase.name} 处理成功:`, result);
        } catch (error) {
            console.error(`❌ ${testCase.name} 处理失败:`, error);
        }
    }
}

// 运行测试
testBankOperations().then(() => {
    console.log('\n🎉 所有测试完成！');
}).catch(error => {
    console.error('\n💥 测试过程中发生错误:', error);
});
